﻿using MyAppAPI.Context;
using MyAppAPI.Models.Pagination;
using MyAppAPI.Models.UserInfoModel;
using MyAppAPI.Repositories.interfaceRepo;

namespace MyAppAPI.Repositories
{
    public class UserInfoRepository: IUserInfoRepository
    {
        public UserContext userInfoContext;
        public UserInfoRepository(UserContext userInfoContext)
        {
            this.userInfoContext = userInfoContext;
        }
        public UserInfoData GetAllUser(string? name, int page, int limit)
        {
            UserInfoData userInfoData=new UserInfoData();
            var result = userInfoContext.UserInfo.Select(o =>new UserInfo
            {
                id= o.id,
                name = o.name,
                addr= o.addr,
                age= o.age,
                birth= o.birth,
                sex= o.sex
            });
            if (!string.IsNullOrEmpty(name))
            {
                result = result.Where(o=>o.name.Contains(name));
            }
            result=result.OrderBy(o=>o.id);
            BasePaginationModel pagination=new BasePaginationModel();
            pagination.PageSize = limit;
            pagination.PageNumber= page;
            result=PaginationService.BasePager<UserInfo>(result,ref pagination);
            List<UserInfo> list=new List<UserInfo>();
            list = result.ToList();
            userInfoData.list= list;
            userInfoData.count = pagination.Total;
            return userInfoData;
        }
    }
}
